# coding=utf-8

import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义方块大小和行列数
BLOCK_SIZE = 30
PADDING_SIZE = 20
ROWS = 20
COLS = 12

# 游戏区域大小
HEIGHT = ROWS * BLOCK_SIZE + PADDING_SIZE * 2
WIDTH = COLS * BLOCK_SIZE + PADDING_SIZE * 2

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# 定义方块的形状
shapes = [
    [[1, 1, 1],
     [0, 1, 0]],

    [[0, 2, 2],
     [2, 2, 0]],

    [[3, 3, 0],
     [0, 3, 3]],

    [[4, 0, 0],
     [4, 4, 4]],

    [[0, 0, 5],
     [5, 5, 5]],

    [[6, 6],
     [6, 6]],

    [[7, 7, 7, 7]]
]

# 定义方块的颜色
colors = [
    BLUE,
    GREEN,
    RED
]

# 定义方块的动作
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)

# 定义方块类
class Block:
    def __init__(self, x=COLS // 2, y=0):
        self.shape = random.choice(shapes)
        self.color = random.choice(colors)
        self.rotation = 0
        self.x = x
        self.y = y

    # 方块移动
    def move(self, pos):
        (x, y) = pos
        self.x += x
        self.y += y

    # 旋转方块
    def rotate(self):
        self.shape = list(zip(*self.shape[::-1]))
        self.rotation = (self.rotation + 1) % 4

# 定义游戏类
class Game:
    def __init__(self):
        pygame.init()
        pygame.display.set_caption("俄罗斯方块")
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        self.font = pygame.font.SysFont(None, 36)
        self.speed = 1
        self.game_over = False
        self.block = Block()

    # 绘制游戏区域
    def draw_grid(self):
        # for x in range(0, WIDTH, BLOCK_SIZE):
        #     pygame.draw.line(self.screen, BLACK, (x, 0), (x, HEIGHT))
        # for y in range(0, HEIGHT, BLOCK_SIZE):
        #     pygame.draw.line(self.screen, BLACK, (0, y), (WIDTH, y))
        
        for i in range(ROWS):
            for j in range(COLS):
                if grid[i][j] != 0:
                    pygame.draw.rect(self.screen, WHITE, [j * BLOCK_SIZE + PADDING_SIZE, i * BLOCK_SIZE + PADDING_SIZE, BLOCK_SIZE, BLOCK_SIZE])
                else:
                    pygame.draw.rect(self.screen, BLACK, [j * BLOCK_SIZE + PADDING_SIZE, i * BLOCK_SIZE + PADDING_SIZE, BLOCK_SIZE, BLOCK_SIZE])

        for i in range(len(self.block.shape)):
            for j in range(len(self.block.shape[i])):
                if self.block.shape[i][j] != 0:
                    pygame.draw.rect(self.screen, self.block.color, [(self.block.x + j) * BLOCK_SIZE + PADDING_SIZE, (self.block.y + i) * BLOCK_SIZE + PADDING_SIZE, BLOCK_SIZE, BLOCK_SIZE])

        pygame.draw.line(self.screen, WHITE, (PADDING_SIZE, PADDING_SIZE), (WIDTH - PADDING_SIZE, PADDING_SIZE))
        pygame.draw.line(self.screen, WHITE, (PADDING_SIZE, PADDING_SIZE), (PADDING_SIZE, HEIGHT - PADDING_SIZE))
        pygame.draw.line(self.screen, WHITE, (PADDING_SIZE, HEIGHT - PADDING_SIZE), (WIDTH - PADDING_SIZE, HEIGHT - PADDING_SIZE))
        pygame.draw.line(self.screen, WHITE, (WIDTH - PADDING_SIZE, PADDING_SIZE), (WIDTH - PADDING_SIZE, HEIGHT - PADDING_SIZE))
        

    # 检查是否可以移动方块
    def can_move(self, pos):
        (x, y) = pos
        for i in range(len(self.block.shape)):
            for j in range(len(self.block.shape[i])):
                if self.block.shape[i][j] != 0:
                    row = self.block.y + y + i
                    col = self.block.x + x + j
                    if row < 0 or col < 0 or col >= COLS or row >= ROWS or grid[row][col] != 0:
                        return False
        return True

    # 方块移动
    def move(self, pos):
        if self.can_move(pos):
            self.block.move(pos)

    # 将方块加入到游戏区域中
    def merge_block(self):
        for i in range(len(self.block.shape)):
            for j in range(len(self.block.shape[i])):
                if self.block.shape[i][j] != 0:
                    row = self.block.y + i
                    col = self.block.x + j
                    grid[row][col] = self.block.shape[i][j]

    # 删除已满的行
    def delete_rows(self):
        for i in range(ROWS):
            if 0 not in grid[i]:
                del grid[i]
                grid.insert(0, [0] * COLS)

    # 检测游戏是否结束
    def check_game_over(self):
        for i in range(COLS):
            if grid[0][i] != 0:
                self.game_over = True

    # 绘制游戏结束画面
    def draw_game_over(self):
        font = pygame.font.SysFont(None, 48)
        text = font.render("Game Over", True, RED)
        self.screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2))

    # 游戏循环
    def run(self):
        global grid
        grid = [[0] * COLS for i in range(ROWS)]
        clock = pygame.time.Clock()
        self.screen.fill(BLACK)

        while not self.game_over:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        self.move(LEFT)
                        print(self.block.x, self.block.y)
                    elif event.key == pygame.K_RIGHT:
                        self.move(RIGHT)
                    elif event.key == pygame.K_DOWN:
                        self.move(DOWN)
                    elif event.key == pygame.K_UP:
                        self.block.rotate()

            # 方块下移
            if self.can_move(DOWN):
                self.block.move(DOWN)
            else:
                self.merge_block()
                self.delete_rows()
                self.check_game_over()
                self.block = Block()

            # 绘制游戏界面
            self.draw_grid()
            if self.game_over:
                self.draw_game_over()

            pygame.display.update()

            # 控制游戏速度
            clock.tick(self.speed)

        # 退出游戏
        pygame.time.wait(2000)
        pygame.quit()
        quit()

# 启动游戏
def start_game():
    game = Game()
    game.run()

if __name__ == "__main__":
    start_game()
